<template>
    <van-nav-bar
            title="订单列表"
            left-text="返回"
            left-arrow
            fixed
            @click-left="$router.push({name: 'user'})"
    ></van-nav-bar>

    <div class="order-list" v-if="!orderIsEmpty">
        <van-cell v-for="order in filteredOrders" :key="order.id">
            <div class="pay-info">
                <span>状态：<span class="status">{{order._status._title}}</span></span>
                <span class="message">{{order._status._msg}}</span>
            </div>
            <div class="product" @click="handleRouter(order)">
                <img :src="order.cartInfo[0].productInfo.image">
                <p class="title show-in-two-lines">{{order.cartInfo[0].productInfo.store_name}}</p>
                <p class="price-and-number">
                    <span class="price">￥{{order.cartInfo[0].truePrice}}</span>
                    <span class="number">数量：{{order.cartInfo[0].cart_num}}</span>
                </p>
            </div>
        </van-cell>
    </div>
    <van-empty v-else description="查询不到订单" />
</template>

<script setup>
  import { ref, defineProps, computed } from 'vue'
  import { useRouter } from 'vue-router'
  import { getOrders } from '@/api/order.js'

  const { type } = defineProps({
    type: String
  })

  const orders = ref([])
  const loadOrders = async () => {
    const { data } = await getOrders()
    orders.value = data
    console.log(data)
  }
  loadOrders()
  const filteredOrders = computed(() => {
    console.log(type)
    if (type !== undefined && type !== "-1") {
      return orders.value.filter(o => o._status._type === Number(type))
    } else {
      return orders.value
    }
  })
  const orderIsEmpty = computed(() => filteredOrders.value.length === 0)

  const router = useRouter()
  const handleRouter = order => {
    router.push({
      name: 'product',
      params:
        {
          productId: order.cartInfo[0].productInfo.id,
        }
    })
  }
</script>

<style lang="scss" scoped>
    .order-list {
        margin-top: 50px;
    }

    .pay-info {
        display: flex;
        justify-content: space-between;

        .status {
            font-weight: 500;
        }

        .message {
            font-size: 12px;
        }
    }

    .product {
        display: flex;

        img {
            width: 88px;
            height: 88px;
            padding-left: 5px;
        }

        .title {
            flex: 1;
            margin: 5px 0 15px;
        }

        .price-and-number {
            display: flex;
            flex-direction: column;
            justify-content: center;
            width: 70px;

            .price {
                font-size: 12px;
                color: #F2270C;
            }

            .number {
                font-size: 10px;
                padding-left: 10px;
            }
        }
    }
</style>
